package com.cartoon.service.impl;

import com.cartoon.entity.Cartoon;
import com.cartoon.dao.CartoonDao;
import com.cartoon.service.CartoonService;
import com.cartoon.util.ResClass;
import com.cartoon.util.ResponseData;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (Cartoon)表服务实现类
 *
 * @author makejava
 * @since 2021-12-10 11:28:08
 */
@Service("cartoonService")
public class CartoonServiceImpl implements CartoonService {
    @Resource
    private CartoonDao cartoonDao;

    @Override
    public ResponseData getCartoonInfo(Long page, Long limit) {
        try{
//            System.out.println((page-1)*limit);
            return new ResponseData("0","请求成功",cartoonDao.getCartoonInfo((page-1)*limit,limit));
        }
        catch (Exception e){
            System.out.println("项目页数page:"+page+";数据条数："+limit);
            e.printStackTrace();

            return new ResponseData("9924","出错啦");
        }
    }

    @Override
    public ResponseData getUpdateCartoon() {
        return new ResponseData("0","请求成功",cartoonDao.getUpdateCartoon());
    }

    @Override
    public ResponseData getCartoonClassificationInfo() {
       try {
           List<Cartoon> classList = new ArrayList<>();
           List<Cartoon> list = cartoonDao.getCartoonClassificationInfo();
           List listType = new ArrayList();
           listType.add("0");
           List listTypeContent = new ArrayList();//总的list,存放每个种类的前五个
           for(int i=0;i<list.size();i++){//将种类的名称存放
               boolean panduan=true;
               for(int ftype=0;ftype<20;ftype++){
                   if(listType.get(ftype).equals(list.get(i).getType().getTypename())){
                       panduan=false;
                       break;
                   }
                   else if(ftype==listType.size()-1 && panduan){
                       listType.add(list.get(i).getType().getTypename());
                       //System.out.println("typezhong"+listType.get(0));
                   }
               }
           }
//         System.out.println(listType.size());
           List<Cartoon> contentList=new ArrayList<>();//存放每个种类的前三个
           for(int controType=0;controType<listType.size();controType++){
               for(int cont=0;cont< list.size();cont++){
//                   boolean test=list.get(cont).getType().getTypename().equals(listType.get(controType))&&contentList.size()<=3;
//                   System.out.println(test);
              if(list.get(cont).getType().getTypename().equals(listType.get(controType))&&contentList.size()<3){
//                      System.out.println(list.get(cont));
                     contentList.add(list.get(cont));
                  }
               }
               listTypeContent.add(new ResClass((String)listType.get(controType),contentList));
               contentList=new ArrayList<>();
           }
           listTypeContent.remove(0);
          // System.out.println(listTypeContent.size());
           return new ResponseData("0","请求成功",listTypeContent);//返回的是存放每个种类前五个的值
       }
       catch (Exception e){
           e.printStackTrace();
           return new ResponseData("9924","出错了！检查哈网络呢");
       }
    }
    public ResponseData getCartoonById(int id){
        try{
            return new ResponseData("0","查询成功",cartoonDao.getCartoonById(id));
        }
        catch (Exception e){
            e.printStackTrace();
            return new ResponseData("9924","出错了");
        }
    }

    @Override
    public ResponseData updateHits(int cartoonId) {
       try{
           cartoonDao.updateHits(cartoonId);
           return new ResponseData("0","更新成功");
       }
       catch (Exception e){
           e.printStackTrace();
           return new ResponseData("9924","更新失败");
       }
    }
}
